package ren.nearby.http.utils.util;

import android.app.Activity;

import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2018/6/27 0027.
 */

public class ActivityUtils {
    private static ActivityUtils instance;
    private Map<String, Activity> activityMap = new HashMap<String, Activity>();

    public static ActivityUtils getInstance() {
        if (instance == null) {
            synchronized (ActivityUtils.class) {
                if (instance == null) {
                    instance = new ActivityUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 保存指定key值的activity（activity启动时调用）
     *
     * @param key
     * @param activity
     */
    public void addActivity(String key, Activity activity) {
        if (activityMap.get(key) == null) {
            Logger.i("add - " + key);
            activityMap.put(key, activity);
        }
    }

    /**
     * 移除指定key值的activity （activity关闭时调用）
     *
     * @param key
     */
    public void delActivity(String key) {
        Activity activity = activityMap.get(key);
        if (activity != null) {
            if (activity.isDestroyed() || activity.isFinishing()) {
                Logger.i("remove - " + key);
                activityMap.remove(key);
                return;
            }
            activity.finish();
            activityMap.remove(key);
            Logger.i("out - " + key);
        }
    }
}
